﻿#include <iostream>
using namespace std;

// 全局变量，具有外部链接，默认存储类为extern
int globalVar;

void function() {
	// 静态变量，具有静态存储期，生命周期贯穿整个程序
	static int staticVar = 20;

	const int constVar = 30; // const变量默认具有static存储期

	// 尝试修改const变量，编译错误
	// constVar = 40;

	// mutable成员变量，可以在const成员函数中修改
	class MyClass {
	public:
		mutable int mutableVar;

		void constMemberFunc() const {
			mutableVar = 50; // 允许修改mutable成员变量
		}
	};

	// 线程局部变量，每个线程有自己的独立副本
	thread_local int threadVar = 60;
}

// 函数声明 
void func(void);
static int count8 = 29; /* 全局变量 */

class Example {
public:
	Example() : value(0), cachedValue(0) {}

	// 常量成员函数
	int getValue() const {
		return value; // 读取常量成员
	}

	// 修改 mutable 成员
	void increment() {
		++value;
		cachedValue = value * 2; // 修改 mutable 成员
	}

	int getCachedValue() const {
		return cachedValue; // 读取 mutable 成员
	}

private:
	int value;                 // 常规成员，不能在 const 函数中修改
	mutable int cachedValue;   // 可修改成员，可以在 const 函数中修改
};

int count00;
extern void write_extern();

int main()
{
	count00 = 5;
	write_extern();

	const Example ex;  // 无法修改（加词） const 对象的成员，无法在 const 对象上调用（加非 const 函数）
	std::cout << "值是 " << ex.getValue() << std::endl;
	std::cout << "Cached Value: " << ex.getCachedValue() << std::endl; // 输出为 0

	while (count8--)
	{
		func();
	}
	extern int externalVar; // 声明具有外部链接的变量

	function();
	   /*
        * 这个程序演示了有符号整数和无符号整数之间的差别
        */
	short int i6;           // 有符号短整数
	short unsigned int j6;  // 无符号短整数

	j6 = 50000;

	i6 = j6;
	cout << i6 << " " << j6;
	std::cout << "\n";

	long int i7;            // 有符号长整数
	long unsigned int j7;   // 无符号长整数
	
	j7 = 100000;

	i7 = j7;
	cout << i7 << " " << j7;
	std::cout << "\n";

	int a = 21;
	int b = 10;
	int c;

	c = a + b;
	cout << "Line 1 - c 的值是 " << c << endl;
	c = a - b;
	cout << "Line 2 - c 的值是 " << c << endl;
	c = a * b;
	cout << "Line 3 - c 的值是 " << c << endl;
	c = a / b;
	cout << "Line 4 - c 的值是 " << c << endl;
	c = a % b;
	cout << "Line 5 - c 的值是 " << c << endl;
	int d = 10;   //  测试自增、自减
	c = d++;
	cout << "Line 6 - c 的值是 " << c << endl;
	d = 10;    // 重新赋值
	c = d--;
	cout << "Line 7 - c 的值是 " << c << endl;

	int a0 = 21;
	int b0 = 10;
	int c0;

	if (a0 == b0)
	{
		cout << "Line 8 - a 等于 b" << endl;
	}
	else
	{
		cout << "Line 8 - a 不等于 b" << endl;
	}
	if (a0 < b0)
	{
		cout << "Line 9 - a 小于 b" << endl;
	}
	else
	{
		cout << "Line 9 - a 不小于 b" << endl;
	}
	if (a0 > b0)
	{
		cout << "Line 10 - a 大于 b" << endl;
	}
	else
	{
		cout << "Line 10 - a 不大于 b" << endl;
	}
	/* 改变 a 和 b 的值 */
	a0 = 5;
	b0 = 20;
	if (a0 <= b0)
	{
		cout << "Line 11 - a 小于或等于 b" << endl;
	}
	if (b0 >= a0)
	{
		cout << "Line 12 - b 大于或等于 a" << endl;
	}

	int a1 = 5;
	int b1 = 20;
	int c1;

	if (a1 && b1)
	{
		cout << "Line 13 - 条件为真" << endl;
	}
	if (a1 || b1)
	{
		cout << "Line 14 - 条件为真" << endl;
	}
	/* 改变 a 和 b 的值 */
	a1 = 0;
	b1 = 10;
	if (a1 && b1)
	{
		cout << "Line 15 - 条件为真" << endl;
	}
	else
	{
		cout << "Line 16 - 条件不为真" << endl;
	}
	if (!(a1 && b1))
	{
		cout << "Line 17 - 条件为真" << endl;
	}

	unsigned int a2 = 60;      // 60 = 0011 1100  
	unsigned int b2 = 13;      // 13 = 0000 1101
	int c2 = 0;

	c2 = a2 & b2;             // 12 = 0000 1100
	cout << "Line 18 - c 的值是 " << c2 << endl;

	c2 = a2 | b2;             // 61 = 0011 1101
	cout << "Line 19 - c 的值是 " << c2 << endl;

	c2 = a2 ^ b2;             // 49 = 0011 0001
	cout << "Line 20 - c 的值是 " << c2 << endl;

	c2 = ~a2;                // -61 = 1100 0011
	cout << "Line 21 - c 的值是 " << c2 << endl;

	c2 = a2 << 2;            // 240 = 1111 0000
	cout << "Line 22 - c 的值是 " << c2 << endl;

	c2 = a2 >> 2;            // 15 = 0000 1111
	cout << "Line 23 - c 的值是 " << c2 << endl;

	int a3 = 21;
	int c3;

	c3 = a3;
	cout << "Line 24 - =  运算符实例，c 的值 = : " << c3 << endl;

	c3 += a3;
	cout << "Line 25 - += 运算符实例，c 的值 = : " << c3 << endl;

	c3 -= a3;
	cout << "Line 26 - -= 运算符实例，c 的值 = : " << c3 << endl;

	c3 *= a3;
	cout << "Line 27 - *= 运算符实例，c 的值 = : " << c3 << endl;

	c3 /= a3;
	cout << "Line 28 - /= 运算符实例，c 的值 = : " << c3 << endl;

	c3 = 200;
	c3 %= a3;
	cout << "Line 29 - %= 运算符实例，c 的值 = : " << c3 << endl;

	c3 <<= 2;
	cout << "Line 30 - <<= 运算符实例，c 的值 = : " << c3 << endl;

	c3 >>= 2;
	cout << "Line 31 - >>= 运算符实例，c 的值 = : " << c3 << endl;

	c3 &= 2;
	cout << "Line 32 - &= 运算符实例，c 的值 = : " << c3 << endl;

	c3 ^= 2;
	cout << "Line 33 - ^= 运算符实例，c 的值 = : " << c3 << endl;

	c3 |= 2;
	cout << "Line 34 - |= 运算符实例，c 的值 = : " << c3 << endl;

	cout << "Size of char : " << sizeof(char) << endl;
	cout << "Size of int : " << sizeof(int) << endl;
	cout << "Size of short int : " << sizeof(short int) << endl;
	cout << "Size of long int : " << sizeof(long int) << endl;
	cout << "Size of float : " << sizeof(float) << endl;
	cout << "Size of double : " << sizeof(double) << endl;
	cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

	// 局部变量声明
	int x, y = 10;

	x = (y < 10) ? 30 : 40;

	cout << "x的值是 " << x << endl;

	int i, j;

	j = 10;
	i = (j++, j + 100, 999 + j);

	cout << i;
	std::cout << "\n";

	double a5 = 21.09399;
	float b5 = 10.20;
	int c5;

	c5 = (int)a5;
	cout << "Line 35 - (int)a 的值是" << c5 << endl;

	c5 = (int)b5;
	cout << "Line 36 - (int)b 的值是" << c5 << endl;

	int  var;
	int* ptr;
	int  val;

	var = 3000;

	// 获取 var 的地址
	ptr = &var;

	// 获取 ptr 的值
	val = *ptr;
	cout << "Value of var :" << var << endl;
	cout << "Value of ptr :" << ptr << endl;
	cout << "Value of val :" << val << endl;

	int a4 = 20;
	int b4 = 10;
	int c4 = 15;
	int d4 = 5;
	int e4;

	e4 = (a4 + b4) * c4 / d4;      // ( 30 * 15 ) / 5
	cout << "Line 37 - (a + b) * c / d 运算符优先级实例的值是 " << e4 << endl;

	e4 = ((a4 + b4) * c4) / d4;    // (30 * 15 ) / 5
	cout << "Line 37 - ((a + b) * c) / d 运算符优先级实例的值是 " << e4 << endl;

	e4 = (a4 + b4) * (c4 / d4);   // (30) * (15/5)
	cout << "Line 39 - (a + b) * (c / d) 运算符优先级实例的值是 " << e4 << endl;

	e4 = a4 + (b4 * c4) / d4;     //  20 + (150/5)
	cout << "Line 40 - a + (b * c) / d 运算符优先级实例的值是 " << e4 << endl;

	int a9 = 21;
	int c9;

	// a 的值在赋值之前不会自增
	c9 = a9++;
	cout << "Line 41 - a++ 的值是 " << c9 << endl;

	// 表达式计算之后，a 的值增加 1
	cout << "Line 42 - a的值是 " << a9 << endl;

	// a 的值在赋值之前自增
	c9 = ++a9;
	cout << "Line 43 - ++a的值是 " << c9 << endl;
	return 0;
}

extern int count00;

void write_extern(void)
{
	std::cout << "Count的值是 " << count00 << std::endl;
}

// 函数定义
void func(void)
{
	static int i8 = 29; // 局部静态变量
	i8++;
	std::cout << "变量 i 为 " << i8;
	std::cout << " , 变量 count 为 " << count8 << std::endl;
}